package com.tensquare.spit.service;


import com.tensquare.entity.*;
import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WF on 2020/12/16 17:40
 */
@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    //1.查询所有
    public List<Spit> findAll() {
        return spitDao.findAll();
    }

    //2.查询单个吐槽
    public Spit findById(String spitId) {
        return spitDao.findById(spitId).get();
    }

    //3.添加吐槽
    public void add(Spit spit) {
        //3.1)设置要添加的主键
        spit.set_id(idWorker.nextId() + "");
        //3.2)添加
        spitDao.save(spit);
    }

    //4.修改吐槽
    public void update(Spit spit) {
        spitDao.save(spit);
    }

    //5.删除吐槽
    public void delete(String spitId) {
        spitDao.deleteById(spitId);
    }

    //6.根据上级id查询吐槽列表
    public PageResult<Spit> comment(String parentid, Integer page, Integer size) {
        //6.1)根据父id进行分页查询
        Page<Spit> spitPage = spitDao.findByParentid(parentid, PageRequest.of(page-1,size));
        //6.2)返回自定义分页对象
        return new PageResult<>(spitPage.getTotalElements(),spitPage.getContent());
    }

    /**
     * 7.点赞处理(方法一：传统方法)
     * @param spitId   需要点赞的吐槽用户的id
     * @return         是否点赞成功 true:成功 flase:点赞失败
     */
    public boolean thumbup(String spitId) {
        //7.0)从redis中得到一个在第一次点赞成功后放入的数据，如果存在，证明点赞过
        Object obj = redisTemplate.opsForValue().get("thumbup_" + spitId);
        if(obj == null){        //此时代表未曾点赞过
            //7.1)根据id查询点赞对象
            Spit spit = spitDao.findById(spitId).get();
            //7.2)修改点赞数
            spit.setThumbup(spit.getThumbup() + 1);
            //7.3)重新保存回去
            spitDao.save(spit);
            //7.4)将一个数据放到redis中，代表是否点赞过
            redisTemplate.opsForValue().set("thumbup_"+spitId,"test");
            return true;
        }
       return false;
    }

    /**
     * 8.点赞处理（方法二：利用MongoTemplate处理）
     * @param spitId
     * @return
     */
    public boolean thumbup2(String spitId) {
        //8.0)从redis中得到一个在第一次点赞成功后放入的数据，如果存在，证明点赞过
        Object obj = redisTemplate.opsForValue().get("thumbup001_" + spitId);
        //8.1)判断是否有值
        if(obj == null){        //没有点赞过
            //8.1.1)定义查询条件
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spitId));
            //8.1.2)定义修改的值对象
            Update update = new Update();
            update.inc("thumbup",1);
            mongoTemplate.updateFirst(query,update,"spit");  //参数3：代表修改的集合名称
            //8.1.3)保存到redis中
            redisTemplate.opsForValue().set("thumbup001_" + spitId,"aaa");
            return true;
        }
        return false;

    }

}
